package com.hmy.contract.manager.order;

import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.order.api.bo.request.ContractInfoUpdateApiRequest;
import com.hmy.order.api.bo.request.ContractOrderStatusUpdateApiRequest;
import com.hmy.order.api.bo.request.UpdateAuthorizerRequest;
import com.hmy.order.api.bo.request.OrderListInfoAndSupplyQueryApiRequest;
import com.hmy.order.api.bo.response.OrderContractInfoUpdateApiResponse;
import com.hmy.order.api.bo.response.UpdateAuthorizerResponse;
import com.hmy.order.api.bo.response.OrderPrimaryAndSupplyApiResponse;
import com.hmy.order.api.client.OrderServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description
 * @date 2023/12/28 19:19
 */

@Component
@Slf4j
public class OrderFeignClientManager {

    @Resource
    private OrderServiceClient orderServiceClient;

    /**
     * 更新订单系统的合同编号
     *
     * @param contractInfo 入参
     * @return {@link OrderContractInfoUpdateApiResponse}
     */
    public FeignResult<OrderContractInfoUpdateApiResponse> updateContractNo(ContractInfoUpdateApiRequest contractInfo) {
        log.info("OrderFeignClientRpc.updateContractNo params:{}", JSONUtil.toJsonPrettyStr(contractInfo));
        FeignResult<OrderContractInfoUpdateApiResponse> response = null;
        response = orderServiceClient.updateContractNo(contractInfo);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000007);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("OrderFeignClientRpc.updateContractNo response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 更新合同状态
     *
     * @param orderStatusInfo 更新合同状态入参
     * @return {@link OrderContractInfoUpdateApiResponse}
     */
    public FeignResult<OrderContractInfoUpdateApiResponse> updateContractOrderStatus(ContractOrderStatusUpdateApiRequest orderStatusInfo) {
        FeignResult<OrderContractInfoUpdateApiResponse> response = null;
        log.info("OrderFeignClientRpc.updateContractOrderStatus params:{}", JSONUtil.toJsonPrettyStr(orderStatusInfo));
        response = orderServiceClient.updateContractOrderStatus(orderStatusInfo);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000015);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("OrderFeignClientRpc.updateContractOrderStatus response:{}", JSONUtil.toJsonPrettyStr(response));
        return response;
    }

    /**
     * 更新订单授权人
     *
     * @param updateAuthorizerRequest 更新授权人请求
     */
    public void updateAuthorizer(UpdateAuthorizerRequest updateAuthorizerRequest) {
        log.info("OrderFeignClientRpc.updateAuthorizer params:{}", updateAuthorizerRequest);
        FeignResult<UpdateAuthorizerResponse> response = orderServiceClient.updateAuthorizer(updateAuthorizerRequest);
        if (response.isFailed()) {
            log.error("OrderFeignClientRpc.updateAuthorizer fail {},{}",response.getMsgCode(),response.getErrorText());
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("OrderFeignClientRpc.updateAuthorizer response:{}", response);
    }

    public List<OrderPrimaryAndSupplyApiResponse> getOrdersInfoAndSupplyInfoByIds(OrderListInfoAndSupplyQueryApiRequest request) {
        FeignResult<List<OrderPrimaryAndSupplyApiResponse>> response = null;
        log.info("OrderFeignClientRpc.getOrdersInfoAndSupplyInfoByIds params:{}", JSONUtil.toJsonPrettyStr(request));
        response = orderServiceClient.getOrdersInfoAndSupplyInfoByIds(request);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000015);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("OrderFeignClientRpc.getOrdersInfoAndSupplyInfoByIds response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }
}
